<template>
  <router-view />
</template>

<script setup lang="ts">
import useSettingsStore from '@/store/modules/settings'
import { handleThemeStyle } from '@/utils/theme'
import { getOaTokenReq } from './api/login';
import useUserStore from './store/modules/user';
import { setToken } from './utils/auth';

const query = new URLSearchParams(location.search);
const fromOA = query.get('fromOA')
const code = query.get('code')
const state = query.get('state')
if(fromOA) {
  useUserStore().logOut().then(() => {
    location.href = `http://cloud.zfsoft.com:8025/zfca/oauth2.0/authorize?response_type=code&client_id=Wh2zjlv4z6MtN0Gyi9&redirect_uri=${import.meta.env.VITE_APP_HOST}`
  })
}

// 获取OA的token
const {run: getOaToken} = useRequest(getOaTokenReq, {
  manual: true,
  onSuccess: async (res) => {
    await setToken(res)
    location.href= import.meta.env.VITE_APP_HOST
  }
})

// OA回调地址会带上code和state
if(code && state) {
  getOaToken({code})
}

onMounted(() => {
  nextTick(() => {
    // 初始化主题样式
    handleThemeStyle(useSettingsStore().theme)
  })
})
</script>
